package de.ba.se.roboter.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

import javax.swing.JPanel;

import de.ba.se.roboter.data.Orienation;
import de.ba.se.roboter.data.Robot;

/**
 * In dieser Klasse wird das Feld des {@link Robot} grafisch dargestellt.
 * 
 * @author Sebastian Tönse
 *
 */
public class RobotPanel extends JPanel {
	
//	private static final Polygon ROBOT_NORTH = new Polygon(new int[] {30, 15, 45}, new int[] {15, 45, 45}, 3);
//	private static final Polygon ROBOT_EAST = new Polygon(new int[] {15, 45, 15}, new int[] {15, 30, 45}, 3);
//	private static final Polygon ROBOT_SOUTH = new Polygon(new int[] {15, 45, 30}, new int[] {15, 15, 45}, 3);
//	private static final Polygon ROBOT_WEST = new Polygon(new int[] {45, 15, 45}, new int[] {15, 30, 45}, 3);
	
	/** @see java.io.Serializable */
	private static final long serialVersionUID = 532847065133029845L;
	
	private boolean field[][] = new boolean[1][1];
	
	private Orienation orienation = Orienation.NORTH;
	
	private Point point = new Point();
	
	public static final int FIELD_SIZE = 42;
	
	public static final int PADDING = 10;
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		for (int i = 0; i < field.length; i++) {
			for (int j = 0; j < field[0].length; j++) {
				g.setColor(Color.BLACK);
				if(field[i][j]) {
					g.fillRect(i * FIELD_SIZE + PADDING, j * FIELD_SIZE + PADDING, FIELD_SIZE, FIELD_SIZE);
				} else {
					g.drawRect(i * FIELD_SIZE + PADDING, j * FIELD_SIZE + PADDING, FIELD_SIZE, FIELD_SIZE);
				}
				if (this.point.equals(new Point(i, j))) {
					g.setColor(Color.BLUE);
					g.fillPolygon(createRobotOnUi(orienation, point.x, point.y));
				}
			}
		}
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(field.length * FIELD_SIZE + 2 * PADDING, field[0].length * FIELD_SIZE + 2 * PADDING);
	}
	
	/**
	 * Gibt den {@link Robot} als {@link Polygon} zurück, um ihn auf dem Bildschirm darstellen zu können.
	 * @param orienation Initiale Blickrichtung
	 * @param x x-Koordinate
	 * @param y y-Koordinate
	 * @return Das {@link Polygon}, das den {@link Robot} darstellt.
	 */
	private Polygon createRobotOnUi(final Orienation orienation, final int x, final int y) {
		final int xOnUi = x * FIELD_SIZE;
		final int yOnUi = y * FIELD_SIZE;
		switch (orienation) {
		case NORTH:
			return new Polygon(new int[] {xOnUi + 20 + PADDING, xOnUi + 5 + PADDING, xOnUi + 35 + PADDING}, new int[] {yOnUi + 5 + PADDING, yOnUi + 35 + PADDING, yOnUi + 35 + PADDING}, 3);
		case EAST:
			return new Polygon(new int[] {xOnUi + 5 + PADDING, xOnUi + 35 + PADDING, xOnUi + 5 + PADDING}, new int[] {yOnUi + 5 + PADDING, yOnUi + 20 + PADDING, yOnUi + 35 + PADDING}, 3);
		case SOUTH:
			return new Polygon(new int[] {xOnUi + 5 + PADDING, xOnUi + 35 + PADDING, xOnUi + 20 + PADDING}, new int[] {yOnUi + 5 + PADDING, yOnUi + 5 + PADDING, yOnUi + 35 + PADDING}, 3);
		case WEST:
			return new Polygon(new int[] {xOnUi + 35 + PADDING, xOnUi + 5 + PADDING, xOnUi + 35 + PADDING}, new int[] {yOnUi + 5 + PADDING, yOnUi + 20 + PADDING, yOnUi + 35 + PADDING}, 3);
		}
		return null;
	}

	/**
	 * Setzt die Daten für die Zeichung des Feldes und des {@link Robot} auf dem Bildschirm.
	 * @param robot Der zu zeigende Robot.
	 */
	public void initUiField(Robot robot) {
		this.field = robot.getField();
		this.point = new Point(robot.getX(), robot.getY());
		this.orienation = robot.getOrienation();
		revalidate();
	}
}
